package microservices

import (
	"fmt"
	"github.com/flairamos/go-component/nacos"
	"github.com/nacos-group/nacos-sdk-go/vo"
	"kuangstudy-mall/srvs/user-srv/global"
)

func NacosRegisterService() {
	port := global.Args.Port
	ip := global.Args.IP

	param := vo.RegisterInstanceParam{
		Ip:          *ip,   // 注册服务的IP
		Port:        *port, // 注册服务的端口
		Enable:      true,
		Healthy:     true,
		Weight:      10,
		Metadata:    map[string]string{"version": "1.0"},
		ClusterName: "DEFAULT",
		GroupName:   global.ServerConfig.NacosInfo.GroupId,
		Ephemeral:   true,
		ServiceName: global.ServerConfig.Name,
	}

	contextPath := "/nacos"
	scheme := "http"
	instance := &nacos.NacosInstance{
		IpAddr:      &global.ServerConfig.NacosInfo.Host,
		Port:        &global.ServerConfig.NacosInfo.Port,
		Username:    &global.ServerConfig.NacosInfo.User,
		Password:    &global.ServerConfig.NacosInfo.Password,
		Scheme:      &scheme,
		ContextPath: &contextPath,
	}

	config := nacos.DefaultClient(global.ServerConfig.NacosInfo.Namespace, global.ServerConfig.Name, global.ServerConfig.NacosInfo.GroupId, instance)
	service := nacos.RegisterInstance(config, param)
	if !service {
		fmt.Println("nacos register failed")
		return
	}
	fmt.Println("nacos register success")
}
